﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;

namespace PeopleEditor
{
  public class PeoplePickerSettings : SPPersistedObject
  {
    #region Private fields
    [Persisted]
    private UserType _settingsForUserType;
    [Persisted]
    private AdminType _typeOfAdmin;
    [Persisted]
    private string _selectionSet;
    [Persisted]
    private bool _onlySearchWithinSiteCollection;
    #endregion

    #region Constructors
    /// <summary>
    /// Default constructor required for persisted object
    /// </summary>
    public PeoplePickerSettings() { }

    /// <summary>
    /// Adds a non admin user PeoplePickerSettings object
    /// </summary>
    /// <param name="settingsForUserType"></param>
    /// <param name="selectionSet"></param>
    public PeoplePickerSettings(string name, SPPersistedObject parent, string selectionSet, bool onlySearchWithinSiteCollection)
      : base(name, parent)
    {
      //Check if name is not null
      if (name==null)
      {
        throw new ArgumentException("Name is null!");
      }
      //Check if parent is not null
      if (parent == null)
      {
        throw new ArgumentException("Parent webapplication is null!");
      }
      //Check if selectionSet is not null
      if (selectionSet == null)
      {
        throw new ArgumentException("SelectionSet is null!");
      }
      //Check if values for selectionSet are OK
      if (!CheckSelectionSet(selectionSet))
      {
        throw new ArgumentException("Invalid SelectionSet value(s)!");
      }
      this._settingsForUserType = UserType.Common;
      this._onlySearchWithinSiteCollection = onlySearchWithinSiteCollection;
      this.SelectionSet = selectionSet;
    }

    /// <summary>
    /// Adds an admin user PeoplePickerSettings object
    /// </summary>
    /// <param name="typeOfAdmin"></param>
    /// <param name="selectionSet"></param>
    public PeoplePickerSettings(string name, SPPersistedObject parent, AdminType typeOfAdmin, string selectionSet, bool onlySearchWithinSiteCollection)
      : base(name, parent)
    {
      //Check if name is not null
      if (name == null)
      {
        throw new ArgumentException("Name is null!");
      }
      //Check if parent is not null
      if (parent == null)
      {
        throw new ArgumentException("Parent webapplication is null!");
      }
      //Check if AdminType is not null
      if (typeOfAdmin == null)
      {
        throw new ArgumentException("Admin type is null!");
      }
      //Check if selectionSet is not null
      if (selectionSet == null)
      {
        throw new ArgumentException("SelectionSet is null!");
      }
      //Check if values for selectionSet are OK
      if (!CheckSelectionSet(selectionSet))
      {
        throw new ArgumentException("Invalid SelectionSet value(s)!");
      }
      this._settingsForUserType = UserType.Admin;
      this.TypeOfAdmin = typeOfAdmin;
      this._onlySearchWithinSiteCollection = onlySearchWithinSiteCollection;
      this.SelectionSet = selectionSet;
    }
    #endregion

    #region Private methods
    /// <summary>
    /// Checks if the selectionset values are valid by comparing the to the
    /// Microsoft.SharePoint.WebControls.PeopleEditor.AccountType enum
    /// </summary>
    /// <param name="selectionSet"></param>
    /// <returns></returns>
    private bool CheckSelectionSet(string selectionSet)
    {
      String[] selectionSetItems = selectionSet.Split(Globals.Delimiter);
      String[] accountTypes = Enum.GetNames(typeof(PeopleEditor.CustPeopleFind.AccountType));

      bool allowedValue = false;
      foreach (string s in selectionSetItems)
      {
        allowedValue = false;
        foreach (string z in accountTypes)
        {
          if (s == z)
          {
            allowedValue = true;
          }
        }
        if (!allowedValue)
        {
          break;
        }
      }

      return allowedValue;
    }
    #endregion

    #region Enumerators
    public enum UserType
    {
      Common,
      Admin
    }

    public enum AdminType
    {
      SiteAdmin,
      WebAdmin
    }
    #endregion

    #region Public properties
    /// <summary>
    /// Gets the type of user these settings are stored for. Returns a PeoplePickerSettings.UserType property.
    /// </summary>
    public UserType SettingsForUserType
    {
      get 
      {
        return _settingsForUserType;
      }
    }

    /// <summary>
    /// Gets or sets the type of administrator user using an PeoplePickerSettings.AdminType property
    /// </summary>
    public AdminType TypeOfAdmin
    {
      get
      {
        return _typeOfAdmin;
      }
      set
      {
        _typeOfAdmin = value;
      }
    }

    /// <summary>
    /// Gets or sets the selectionset
    /// </summary>
    public string SelectionSet
    {
      get
      {
        return _selectionSet;
      }
      set
      {
        _selectionSet = value;
      }
    }

    /// <summary>
    /// Gets the initial value for the PeoplePicker settings when the feature was deployed to the web application
    /// </summary>
    public bool OnlySearchWithinSiteCollection
    {
      get
      {
        return _onlySearchWithinSiteCollection;
      }
    }
    #endregion
  }
}